<?php
/**
 * Created by PhpStorm.
 * User: zhj
 * Date: 2019/3/6
 * Time: 16:36
 */

namespace app\common\logic;
use app\admin\logic\Log;
/**
 * 留言逻辑
 */
class Msg extends LogicBase
{

    /**
     * 获取留言列表
     */
    public function getMsgList($where = [], $field = '*', $order = '',$pagenation='15')
    {

        /* $this->modelMsg->alias('a');
 
         $join = [
             [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
             [SYS_DB_PREFIX . 'Msg_category c', 'a.category_id = c.id'],
         ];
 
         $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
 
         $this->modelMsg->join = $join;*/

        return $this->modelMsg->getList($where, $field, $order,$pagenation);
    }

    /**
     * 获取留言列表搜索条件
     */
    public function getWhere($data = [])
    {

        $where = [];

        !empty($data['search_data']) && $where['a.title|a.content'] = ['like', '%'.$data['search_data'].'%'];

        return $where;
    }

    /**
     * 留言信息编辑
     */
    public function MsgEdit($data = [])
    {

        $validate_result = $this->validateMsg->scene('edit')->check($data);

        if (!$validate_result) {

            return [RESULT_ERROR, $this->validateMsg->getError()];
        }

        $url = url('MsgList');

        $data['content'] = $data['content'];

        $result = $this->modelMsg->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        //$result && action_log($handle_text, '留言' . $handle_text . '，name：' . $data['name']);

        return $result ? [RESULT_SUCCESS, '留言操作成功', $url] : [RESULT_ERROR, $this->modelMsg->getError()];
    }

    /**
     * 获取留言信息
     */
    public function getMsgInfo($where = [], $field = '*')
    {

        /* $this->modelMsg->alias('a');
 
         $join = [
             [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
             [SYS_DB_PREFIX . 'Msg_category c', 'a.category_id = c.id'],
         ];
 
         $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
 
         $this->modelMsg->join = $join;*/

        return $this->modelMsg->getInfo($where, $field);
    }

    /**
     * 留言分类删除
     */
    public function MsgCategoryDel($where = [])
    {

        $result = $this->modelMsgCategory->deleteInfo($where);

        $result && action_log('删除', '留言分类删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '留言分类删除成功'] : [RESULT_ERROR, $this->modelMsgCategory->getError()];
    }

    /**
     * 留言删除
     */
    public function MsgDel($where = [])
    {

        $result = $this->modelMsg->deleteInfo($where);

        $result && action_log('删除', '留言删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '留言删除成功'] : [RESULT_ERROR, $this->modelMsg->getError()];
    }
}
